/**
 * @class   wosh::network::ServerTcp
 * @brief   The class provides a TCP-based server, it handles
 *          incoming TCP connections.
 *
 ****************************************************************************
 * @version 0.8.377 $Id: ServerTcp.h 2850 2010-08-06 18:49:18Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Network_ServerTcp_H__
 #define __WOSH_Network_ServerTcp_H__

 #include <core/Object.h>
 #include <core/LogContext.h>


namespace wosh {
 namespace network {

// #define _ServerTcp_DEBUG_ENABLED

class ServerTcp;
class SocketTcp;

class IServerTcpListener {
	public:
		virtual void serverTcp_incomingConnection( int socket_id, wosh::network::ServerTcp* source ) = 0;

	public:
		virtual ~IServerTcpListener () { }

}; // class def



class ServerTcp : public IReflection
 {
	WOSH_CLASSNAME(wosh::network::ServerTcp)
	WOSH_CLASS_ABSTRACT(true)

	public:
		ServerTcp();

		virtual ~ServerTcp() { }

	public:
		virtual WRESULT bindSocket( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;
		virtual WRESULT unbindSocket( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;
		virtual bool isListening() const = 0;

	public:
		virtual WRESULT bindSocket( const std::string& address_port, unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT bindSocket( const std::string& address, int port, unsigned long timeout = LIMIT_ULONG_MAX );

/** @name Getters
 * @{
 ******************************************************************************/
	public:
 		inline const std::string& getAddress() const				{ return this->address; }
		inline int getPort() const									{ return this->port; }
		inline const std::string& getAddressPort() const 			{ return this->address_port; }

		wosh::network::IServerTcpListener* getListener() 			{ return this->listener; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		bool setAddressPort( const std::string& add_value, int port_value );
		bool setAddressPort( const std::string& value );

		virtual void setMaxPendingConnections( int count ) = 0;

		void setListener( wosh::network::IServerTcpListener* tcp_listener ) 		{ this->listener = tcp_listener; }
//@}
	protected:
		std::string address;
		int port;
		std::string address_port; // shadow var

		wosh::network::IServerTcpListener* listener;

		friend class wosh::network::SocketTcp;

#ifdef _ServerTcp_DEBUG_ENABLED
		mutable wosh::LogContext Log;
#endif


}; // class def

 }; // namespace network
}; // namespace wosh

#endif //__WOSH_Network_ServerTcp_H__
